import Vue from 'vue'
import Router from 'vue-router'
import Index from './views/Index.vue'
import Login from './views/Login.vue'
import Register from './views/Register.vue'
import NotFound from './views/Not404.vue'
import Indexdown from './views/Indexdown.vue'
import Mapp from './views/Mapp.vue'
import Person from './views/Person.vue'
import Pmypwd from './views/Pmypwd.vue'
import Pmy from './views/Pmy.vue'
import Pmyo from './views/Pmyo.vue'
import Phead from './views/Phead.vue'
import Discount from './views/Discount.vue'
import DisDetails from './views/DisDetails.vue'
import Cart from './views/Cart.vue'
import Menu from './views/Menu.vue'
import Menudisdetails from './views/Menudisdetails.vue'
import Evaluate from './views/Evaluate.vue'
import Pay from './views/Pay.vue'
import Writeva from './views/Writeva.vue'
import Pmyorder from './views/Pmyorder.vue'

Vue.use(Router)
let router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      redirect: '/index/indexdown' //路由跳转
    },
    {
      path: '/pmyorder',
      name: 'pmyorder',
      component: Pmyorder,
    },
    {
      path: '/index',
      name: 'index',
      component: Index,
      children: [
        {
          path: 'indexdown',
          name: 'indexdown',
          component: Indexdown,
        },
        
        {
          path: 'mapp',
          name: 'mapp',
          component: Mapp,
        },
        {
          path: 'menu',
          name: 'menu',
          component: Menu,
        },
        {
          path: 'menudisdetails/:tradeID',
          name: 'menudisdetails',
          component: Menudisdetails,
          children: [

          ]
        },
        {
          path: 'writeva',
          name: 'writeva',
          component: Writeva,
          children: [

          ]
        },
        {
          path: 'pay',
          name: 'pay',
          component: Pay,
          children: [

          ]
        },
        {
          path: 'evaluate/:tradeID',
          name: 'evaluate',
          component: Evaluate,
        },
        {
          path: 'person',
          name: 'person',
          component: Person,
          children: [
            {
              path: 'pmypwd',
              name: 'pmypwd',
              component: Pmypwd,
            },
            {
              path: 'pmy',
              name: 'pmy',
              component: Pmy,
            },
            {
              path: 'pmyo',
              name: 'pmyo',
              component: Pmyo,
            },
            {
              path: 'phead',
              name: 'phead',
              component: Phead,
            },
          ]
        },
        {
          path: 'discount',
          name: 'discount',
          component: Discount,
        },
        {
          path: 'disDetails',
          name: 'disDetails',
          component: DisDetails
        },
        {
          path: 'cart',
          name: 'cart',
          component: Cart
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: Login
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      // component: function () { 
      //   return import(/* webpackChunkName: "about" */ './views/About.vue')
      // }
    },
    {
      path: '/register',
      name: 'register',
      component: Register
    },
    {
      path: '/*',   //路由匹配不成功时
      name: 'notfound',
      component: NotFound
    }
  ]
})
// 设置路由守卫
router.beforeEach((to,from,next) => {
  // 除了login和register，其他的路由访问必须先登录
 let tokenIsExists = localStorage.getItem('mytoken') ? true : false //检查本地存储中是否有token
 if(to.path == '/login' || to.path == '/register' ||to.path == '/index/indexdown'){
   next()    //允许访问路由
 }else{
   if(tokenIsExists){
     next()  //已经登录并取得token，允许访问路由
   }else{
     next('/login')  //路由跳转到登录组件
   }
 }
})
export default router